package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @return R<String>
     * @author Mosetian
     * @date 2022-9-12 11:20
     * @param    category
     */
    @PostMapping
    public R<String> add(@RequestBody Category category, HttpSession session) {
//
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        //从session中获取当前登录人
//        Employee employee = (Employee) session.getAttribute("loginEmployee");
//        log.info(employee.toString());
//        category.setCreateUser(employee.getId());
//        category.setUpdateUser(employee.getId());

        boolean save = categoryService.save(category);
        return save ? R.success("新增分类成功") : R.error("新增分类失败");
    }

    /**
     * 分类分页查询
     *
     * @return R<Map>
     * @author Mosetian
     * @date 2022-9-12 11:35
     * @param    page
     * @param    pageSize
     */
    @GetMapping("/page")
    public R<Map> Page(int page, int pageSize) {
        Page<Category> pageInfo = new Page<>(page, pageSize);

        categoryService.page(pageInfo);

        long total = pageInfo.getTotal();//总记录数
        List<Category> records = pageInfo.getRecords();//列表数据

        Map map = new HashMap();
        map.put("total", total);
        map.put("records", records);

        return R.success(map);
    }

    /**
     * 删除分类,根据id
     * @return R<String>
     * @author Mosetian
     * @date 2022-9-12 11:52
     * @param    id
     */
    @DeleteMapping
    public R deleteById(Long id) {

        //调用一个自己定义的service方法,里面进行是否关联的判断
        R result = categoryService.remove(id);

        return result;
    }

    /**
     * 修改分类,根据id
     * @author Mosetian
     * @date 2022-9-12 12:05
     * @param	category
     * @return R<String>
     */
    @PutMapping
    public R update(@RequestBody Category category, HttpSession session) {

//        category.setUpdateTime(LocalDateTime.now());
//        //从session中获取当前登录人
//        Employee employee = (Employee) session.getAttribute("loginEmployee");
//        category.setUpdateUser(employee.getId());


        boolean flag = categoryService.updateById(category);

        return flag ? R.success("修改分类信息成功") : R.error("修改分类信息失败");
    }

    @GetMapping("/list/{type}")
    public R<List> getList(@PathVariable int type){
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        queryWrapper.eq(Category::getType, type);
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("/list")
    public R<List> getList(){

        List<Category> list = categoryService.list();
        return R.success(list);
    }
}
